import untils from '../../utils/util' //相对地址
import {request} from "../../request/index.js"

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pwd: '',
    noinput: true,
    pwdinput: false,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    array1: [],
    value1: 0,
    managerName: '',
    return: {
      isPhoneFlag: true, // 联系方式手机号状态
    },
    userInfo:{},
    msg: {
      isunread: true,
      unreadNum: 2
    }

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    }) 
    
  },
  bindChange(e) {
    if (e.target.dataset.key) {
      if (e.target.dataset.key == 'formOne.tele') {
        this.setData({
          isPhoneFlag: untils.isPhone(e.detail.value)
        })
        if (!this.data.isPhoneFlag) {
          wx.showToast({
            title: '联系方式格式有误',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    }
    this.setData({
      [e.target.dataset.key]: e.detail.value
    })
  },
  bindPicker1Change: function (e) {
    this.setData({
      value1: e.detail.value
    })
    this.setData({
      managerName: this.data.array1[e.detail.value]
    })
    this.setData({
      no: e.detail.value
    });
    this.setData({
      noinput: true
    });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({
        disabled: false
      });
    }
  },
  pwdinput: function (e) {
    this.setData({
      pwd: e.detail.value
    });
    this.setData({
      pwdinput: true
    });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({
        disabled: false
      });
    }
  },
  formSubmit: function (e) {
    this.setData({
      isPhoneFlag: untils.isPhone(e.detail.value.pwd)
    })
    if (!this.data.isPhoneFlag) {
      wx.showToast({
        title: '手机号填写错误',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.showLoading({
        title: '登录中...',
      })
      console.log(e);
      this.setData({
        disabled: true
      });
      request({
        url: "/user/wherherUser", 
        data: {
          managerName: this.data.managerName,
          telephone: e.detail.value.pwd
        }, 
        header: {
          'content-type': 'application/json' // 默认值
        }
      })
      .then(res => {
        if (res.statusCode == 200) {
          wx.setStorageSync('user', res.data.managerName)
          wx.setStorageSync('tel', e.detail.value.pwd)
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.switchTab({
              url: '../features/features',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    //查询managerName列表
    request({
      url: "/user/queryUserName", 
      data: {
      }, 
      header: {
        'content-type': 'application/json' // 默认值
      }
    })
    .then(res => {
      that.setData({
        array1: res.data,
        managerName: res.data[0]
      })
    })
  },

  clean:function(){
    wx.setStorageSync('user', "")
  },

  onShow: function () {
    if (this.data.no == '' || this.data.pwd == '') {
      this.setData({
        disabled: true
      });
    } else {
      this.setData({
        disabled: false
      });
    }
  }
})